package sk.tramtatiri.data.dao.jpa;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import javax.persistence.Query;
import javax.persistence.TypedQuery;

import org.springframework.orm.jpa.JpaCallback;
import org.springframework.transaction.annotation.Transactional;

import sk.tramtatiri.data.dao.interfaces.UserSportDao;
import sk.tramtatiri.data.dataobjects.UserSport;

public class UserSportDaoJPAImp extends AbstractDaoJPAImpl<UserSport> implements
		UserSportDao {

	public UserSportDaoJPAImp() {
		super(UserSport.class);
	}

	public List<UserSport> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	public int countAll() {
		// TODO Auto-generated method stub
		return 0;
	}

	public UserSport findName(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	@Transactional
	public List<Object[]> sumUserCaloriesLastXDays(final long userId,
			final int days) {
		Calendar c = Calendar.getInstance();
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH),
				c.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		c.add(Calendar.DAY_OF_MONTH, -days);
		final Date date = new Date(c.getTimeInMillis());

		return getJpaTemplate().execute(new JpaCallback<List<Object[]>>() {
			public List<Object[]> doInJpa(EntityManager em)
					throws PersistenceException {
				Query query0 = null;
				if (days < 99) {
					query0 = em
							.createQuery("select us.date,sum(us.totalCalories) from UserSport us where us.userId = :userId and us.date >= :startDate group by us.date order by us.date asc");
					query0.setParameter("startDate", date);
				} else {
					query0 = em
							.createQuery("select us.date,sum(us.totalCalories) from UserSport us where us.userId = :userId group by us.date order by us.date asc");
				}
				query0.setParameter("userId", userId);
				return query0.getResultList();
			}
		});
	}

	@Transactional
	public int sumUserToday(final long user_id) {
		Calendar c = Calendar.getInstance();
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH),
				c.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
		final Date date = new Date(c.getTimeInMillis());
		try {
			return getJpaTemplate().execute(new JpaCallback<Integer>() {
				public Integer doInJpa(EntityManager em)
						throws PersistenceException {
					TypedQuery<Long> query0 = em
							.createQuery(
									"select sum(us.totalCalories) from UserSport us where us.user_id = :userId and us.date = :today",
									Long.class);
					query0.setParameter("today", date);
					query0.setParameter("userId", user_id);
					return (query0.getSingleResult()).intValue();
				}
			});
		} catch (Exception e) {
			return 0;
		}
	}
	
	@Transactional
	public List<Object[]> findAllByDate(final long user_id, final Date date) {
		return getJpaTemplate().execute(new JpaCallback<List<Object[]>>() {
			public List<Object[]> doInJpa(EntityManager em)
					throws PersistenceException {
				
					Query query0 = em
							.createQuery("select s,us from UserSport us, Sport s where s.id = us.sport_id and us.user_id = :userId and us.date = :date order by s.name asc");
					query0.setParameter("date", date);
				
				query0.setParameter("userId", user_id);
				return query0.getResultList();
			}
		});
	}
	
	@Transactional
	public void delete(UserSport object) {
		UserSport us = getJpaTemplate().find(UserSport.class, object.getId());
		getJpaTemplate().remove(us);
	}
}
